package com.catering.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.dao.CateringOrderDao;
import com.catering.dao.CateringUserDao;
import com.catering.dao.LoveUserDao;
import com.catering.entity.CateringUser;
import com.catering.entity.LoveUser;
import com.catering.service.CateringUserService;
import com.catering.until.CommonUtils;
import com.catering.until.ResultData;
import com.catering.until.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CateringUserServiceImpl extends ServiceImpl<CateringUserDao, CateringUser> implements CateringUserService {
	
	@Autowired
	private CateringUserDao cateringUserDao;
	@Autowired
	private CateringOrderDao cateringOrderDao;
	@Autowired
	private LoveUserDao loveUserDao;

	@Override
	public ResultData select(Map<String,String> map) {
		try {
			QueryWrapper wrapper = CommonUtils.getKeySet(map,"");
			wrapper.orderByDesc("create_time");
			List<CateringUser> list = new ArrayList<CateringUser>();
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringUser> page = new Page<>(Integer.valueOf(map.get("page")),Integer.valueOf(map.get("limit")));
				IPage<CateringUser> pageList = cateringUserDao.selectPage(page, wrapper);
				Integer count = cateringUserDao.selectCount(wrapper);
				list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				list = cateringUserDao.selectList(wrapper);
				return ResultData.success(list);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.SERVER_ERROR.getCode(),ResultEnum.SERVER_ERROR.getMsg());
	}

	@Override
	public ResultData selectOne(Map<String,String> map) {
		try {
			QueryWrapper wrapper = CommonUtils.getKeySet(map,"");
			if(CommonUtils.checkNull(map.get("opendId"))) return ResultData.error(ResultEnum.SELECT_ERROR.getCode(),"用户ID为空");
			LoveUser user = loveUserDao.selectOne(wrapper);
			if(user != null ){
				CateringUser cateringUser = cateringUserDao.selectOne(new LambdaQueryWrapper<CateringUser>().eq(CateringUser::getUserId,user.getSysId()));
				QueryWrapper w = new QueryWrapper();
				w.eq("user_id",cateringUser.getUserId());
				w.eq("status","1");
				String payMoney = cateringOrderDao.selectSumPay(w);
				cateringUser.setPayMoney(payMoney);
				return ResultData.success(cateringUser);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.SERVER_ERROR.getCode(),ResultEnum.SERVER_ERROR.getMsg());
	}

	@Override
	public ResultData insert(Map<String,String> map) {
		try {
			CateringUser vo = JSON.parseObject(JSON.toJSONString(map), CateringUser.class);
			vo.setCreateTime(CommonUtils.getTime(19));
			cateringUserDao.insert(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}

	@Override
	public ResultData update(Map<String,String> map) {
		try {
			CateringUser vo = JSON.parseObject(JSON.toJSONString(map), CateringUser.class);
			cateringUserDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}

	@Override
	public ResultData delete(Map<String,String> map) {
		try {
			CateringUser vo = JSON.parseObject(JSON.toJSONString(map), CateringUser.class);
			List<String> list = Stream.of(vo.getSysId().split(",")).collect(Collectors.toList());
			cateringUserDao.deleteBatchIds(list);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
	}
}
